package com.qysoft.rapid.core;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.qysoft.rapid.consts.RapidConsts;
import com.qysoft.rapid.plugin.ShiroPlugin;
import com.qysoft.rapid.plugin.druid.DruidPlugin;
import com.qysoft.rapid.plugin.druid.DruidStatViewHandler;
import com.qysoft.rapid.plugin.mybatis.MyBatisPlugin;
import com.qysoft.rapid.shiro.RapidShiroInterceptor;
import org.beetl.ext.jfinal3.JFinal3BeetlRenderFactory;

import java.net.URL;

/**
 * rapid平台配置工具
 * @author liugong
 */
public abstract class RapidConifg extends JFinalConfig{
	
	private Routes routes;

	/**
	 * 配置数据库工具
	 */
	public abstract int configDbTool();

	/**
	 * 配置action
	 * @param me
	 */
	public abstract void configAction(Routes me);
	
	
	/**
	 * 配置action
	 */
	public abstract String configErrorMSg();
	
	
	/**
	 * 配置action
	 */
	public abstract String configSysName();
	
	/**
	 * 设置数据库版本
	 */
	public abstract int configDbType();
	
	
	@Override
	public final void configConstant(Constants me) {
		//设置错误页
		me.setError401View("/pages/common/401.html");
		me.setError404View("/static/pages/wait.html"); 
		me.setError403View("/pages/common/403.html");
		me.setError500View("/pages/common/500.html");
		//读取rapid配置
		loadPropertyFile("config/rapid.properties");
		RapidConsts.setIS_ALL_SCR(getPropertyToBoolean("isAllScr"));
		RapidConsts.setIS_DEV_MODE(getPropertyToBoolean("devMode"));
		me.setDevMode(RapidConsts.isIS_DEV_MODE());
		me.setViewType(ViewType.JSP);
		//配置全局错误提示
		RapidConsts.setERROR_MSG(configErrorMSg());
		//配置系统名称
		RapidConsts.setSYS_NAME(configSysName());
		//配置模版引擎
		JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
		rf.config();
		me.setRenderFactory(rf);
		//以下可以添加自定义函数
//		GroupTemplate gt = rf.groupTemplate;
	}

	@Override
	public final void configRoute(Routes me) {
		this.routes = me;
		//配置页面访问主路径
		me.setBaseViewPath("WEB-INF/pages/modules");
		configAction(this.routes);
	}

	   @Override
    public final void configHandler(Handlers me) {
        //druid监控
		DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid");
        me.add(dvh);
    }

	@Override
	public final void configInterceptor(Interceptors me) {
		// shiro权限拦截器配置
		me.add(new RapidShiroInterceptor());
		// 让 模版 可以使用session
		me.add(new SessionInViewInterceptor());
	}

	@Override
	public final void configPlugin(Plugins me) {
		URL url = getClass().getResource("/config/ehcache.xml");
		me.add(new EhCachePlugin(url));
		me.add(new ShiroPlugin(this.routes));
		int dbType = configDbType();
		RapidConsts.setCURRENT_DB_TYPE(dbType);
		switch (dbType) {
		case RapidConsts.DB_TYPE_ORACLE:
			configOrlace(me);
			break;
		case RapidConsts.DB_TYPE_MYSQL:
			configMySql(me);
			break;
		}
	}

	private void configOrlace(Plugins me){
		//读取数据库配置
		loadPropertyFile("config/oracle.properties");
		DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),getProperty("user"),getProperty("password"));
		dp.setDriverClass(getProperty("driverClass"));
		dp.addFilter(new StatFilter());
		dp.setMaxActive(getPropertyToInt("dbMaxActive"));
		if (RapidConsts.isIS_DEV_MODE()) {
			dp.setInitialSize(1);
		}else {
			dp.setInitialSize(getPropertyToInt("dbInitSize"));
		}
		WallFilter wall = new WallFilter(); 
		wall.setDbType(getProperty("dbType")); 
		dp.addFilter(wall);
		me.add(dp);		
		//配置数据库操作工具
		int dbToll = configDbTool();
		RapidConsts.setCURRENT_DB_TOOL(dbToll);
		switch (dbToll) {
		case RapidConsts.DB_TOOL_ACTIVERECORD:
			//配置ActiveRecord插件
			ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
			me.add(arp);
			arp.setShowSql(RapidConsts.isIS_DEV_MODE());
			// 配置Oracle方言
			arp.setDialect(new OracleDialect());
			// 配置属性名(字段名)大小写不敏感容器工厂
			arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
			break;
		case RapidConsts.DB_TOOL_MYBATIS:
			MyBatisPlugin myBatisPlugin = new MyBatisPlugin(dp);
			me.add(myBatisPlugin);
			myBatisPlugin.setShowSql(RapidConsts.isIS_DEV_MODE());
			break;
		}
	}
	
	
	private void configMySql(Plugins me){
		//读取数据库配置
		loadPropertyFile("config/mysql.properties");
		DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),getProperty("user"),getProperty("password"));
		dp.setDriverClass(getProperty("driverClass"));
		dp.addFilter(new StatFilter());
		dp.setMaxActive(getPropertyToInt("dbMaxActive"));
		if (RapidConsts.isIS_DEV_MODE()) {
			dp.setInitialSize(1);
		}else {
			dp.setInitialSize(getPropertyToInt("dbInitSize"));
		}
		WallFilter wall = new WallFilter(); 
		wall.setDbType(getProperty("dbType")); 
		dp.addFilter(wall);
		me.add(dp);		
		//配置数据库操作工具
		int dbToll = configDbTool();
		RapidConsts.setCURRENT_DB_TOOL(dbToll);
		switch (dbToll) {
		case RapidConsts.DB_TOOL_ACTIVERECORD:
			//配置ActiveRecord插件
			ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
			me.add(arp);
			arp.setShowSql(RapidConsts.isIS_DEV_MODE());
			break;
		case RapidConsts.DB_TOOL_MYBATIS:
			MyBatisPlugin myBatisPlugin = new MyBatisPlugin(dp);
			me.add(myBatisPlugin);
			myBatisPlugin.setShowSql(RapidConsts.isIS_DEV_MODE());
			break;
		}
	}

	@Override
	public void configEngine(Engine engine) {



	}

}
